<template>
  <uni-nav-bar title="奖励记录" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px' }">
    <view class="package" style="padding:0px 20rpx;">
      <view class="lis" v-for="item in orderLists" :key="item.id">
        <view class="space-between">
          <view>{{item.type}}</view>
          <view style="font-size: 32rpx;">{{item.money}}</view>
        </view>
        <view class="space-between top">
          <view>{{item.createtime}}</view>
          <view>{{item.money_type}}</view>
        </view>
      </view>
    </view>
    <view v-if="orderLists.length<1" class="column" :style="{ height: pageHeight + 'px' }">
      <image :src="Empty" style="width:352rpx;height: 352rpx;"></image>
      <text style="margin-top:20rpx;color:#666;">暂无记录</text>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
  import { navBack, navTo } from '@/utils/navigator';
  import { getRewardLog } from '@/gql/administrator';
  import { toPublish } from '@mqtt';
  import { onLoad } from '@dcloudio/uni-app';
  import { showLoading } from '@/utils/prompt';
  import { ref } from 'vue';
  import { Empty } from '@/utils/Static';
  const orderLists = ref([]);
  const isLoading = ref(false);
  const lastPage = ref(1);
  const currentPage = ref(1);

  const pageHeight = ref('')
  const driver_id = ref('')
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = (res.windowHeight - 55).toString();
    },
  });
  onLoad((pearm) => {
    driver_id.value = pearm.id
    init()
  })
  function init() {
    showLoading()
    isLoading.value = true;
    const payload = {
      query: getRewardLog,
      variables: {
        page: currentPage.value,
        driver_id: Number(driver_id.value),
      },
    };
    toPublish(
      'ql/control/getRewardLog',
      payload,
      (obj : any) => {
        const { getRewardLog } = obj.data;
        isLoading.value = false;
        orderLists.value = [...orderLists.value, ...getRewardLog.list];
        lastPage.value = getRewardLog.lastpage;
      }
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      init();
    }
  }
</script>

<style scoped lang="less">
  .lis:nth-last-of-type(1) {
    border-bottom: 0px;
  }

  .lis {
    width: 100%;
    padding: 20rpx 0px;
    box-sizing: border-box;
    border-bottom: 1px solid #eee;
  }

  .top {
    margin-top: 10rpx;
  }
</style>